//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.customer;

import java.math.BigDecimal;
import net.sf.jasperreports.engine.*;
import org.jpgolay.me.item.Item;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.customer:
//            Customer
public class CustomerPrintDataSource
        implements JRDataSource {

    public CustomerPrintDataSource(Customer customer, Object familyDiscount[][], Object itemDiscount[][], Parameter param) {
        this.customer = customer;
        this.familyDiscount = familyDiscount;
        this.itemDiscount = itemDiscount;
        this.param = param;
        countLineFD = 0;
        countLineID = 0;
        count = 41 * (familyDiscount.length + itemDiscount.length);
        crud = new ObjectCRUD();
    }

    public boolean next()
            throws JRException {
        return count > 0;
    }

    public Object getFieldValue(JRField field)
            throws JRException {
        String fieldname = field.getName();
        String value = null;
        if ("Id".equals(fieldname)) {
            value = customer.getId().toString();
        } else if ("Compagny".equals(fieldname)) {
            value = customer.getRaisonSociale();
        } else if ("Status".equals(fieldname)) {
            value = customer.getCivilite();
        } else if ("AddrLn1Bill".equals(fieldname)) {
            value = customer.getAdresseFactUn();
        } else if ("AddrLn2Bill".equals(fieldname)) {
            value = customer.getAdresseFactDeux();
        } else if ("ZipBill".equals(fieldname)) {
            value = customer.getCpFact();
        } else if ("CityBill".equals(fieldname)) {
            value = customer.getVilleFact();
        } else if ("CountryBill".equals(fieldname)) {
            value = customer.getPaysFact();
        } else if ("AddrLn1Delivery".equals(fieldname)) {
            value = customer.getAdresseLivrUn();
        } else if ("AddrLn2Delivery".equals(fieldname)) {
            value = customer.getAdresseLivrDeux();
        } else if ("ZipDelivery".equals(fieldname)) {
            value = customer.getCpLivr();
        } else if ("CityDelivery".equals(fieldname)) {
            value = customer.getVilleLivr();
        } else if ("CountryDelivery".equals(fieldname)) {
            value = customer.getPaysLivr();
        } else if ("Tel".equals(fieldname)) {
            value = customer.getTelephone();
        } else if ("Fax".equals(fieldname)) {
            value = customer.getFax();
        } else if ("Cel".equals(fieldname)) {
            value = customer.getPortable();
        } else if ("Email".equals(fieldname)) {
            value = customer.getEmail();
        } else if ("Web".equals(fieldname)) {
            value = customer.getSiteWeb();
        } else if ("IdGLN".equals(fieldname)) {
            value = customer.getIdGLN();
        } else if ("Date".equals(fieldname)) {
            String date = customer.getUpdateLastTime().substring(6, 8) + "/" + customer.getUpdateLastTime().substring(4, 6) + "/" + customer.getUpdateLastTime().substring(0, 4) + " " + customer.getUpdateLastTime().substring(8, 10) + ":" + customer.getUpdateLastTime().substring(10, 12) + ":" + customer.getUpdateLastTime().substring(12);
            value = date;
        } else if ("Family".equals(fieldname)) {
            value = customer.getFamilleClientId();
        } else if ("Contact".equals(fieldname)) {
            value = customer.getNomPrenomContact();
        } else if ("ContactFunction".equals(fieldname)) {
            value = customer.getFonctionContact();
        } else if ("Bank".equals(fieldname)) {
            value = customer.getBanque();
        } else if ("BankAccount".equals(fieldname)) {
            value = customer.getNumeroCompte();
        } else if ("Notes".equals(fieldname)) {
            value = customer.getCommentaire();
        } else if ("SalesPerson".equals(fieldname)) {
            value = customer.getCommerciauxId().toString();
        } else if ("Discount".equals(fieldname)) {
            value = customer.getRemise().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("FidelityDiscount".equals(fieldname)) {
            value = customer.getRemiseFidelite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("FidelityQuota".equals(fieldname)) {
            value = customer.getCotaFidelite().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("TotalPayment".equals(fieldname)) {
            value = customer.getTotalEncaisser().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("Due".equals(fieldname)) {
            value = customer.getEcheancePaiement().toString();
        } else if ("MethodeOfPayment".equals(fieldname)) {
            value = customer.getModeReglement();
        } else if ("Reference".equals(fieldname)) {
            value = discountList(0);
        } else if ("R".equals(fieldname)) {
            value = discountList(1);
        } else if ("QT1".equals(fieldname)) {
            value = discountList(2);
        } else if ("R1".equals(fieldname)) {
            value = discountList(3);
        } else if ("QT2".equals(fieldname)) {
            value = discountList(4);
        } else if ("R2".equals(fieldname)) {
            value = discountList(5);
        } else if ("QT3".equals(fieldname)) {
            value = discountList(6);
        } else if ("R3".equals(fieldname)) {
            value = discountList(7);
        }
        count--;
        return value;
    }

    private String discountList(int col) {
        if (countLineID < itemDiscount.length) {
            switch (col) {
                case 0: // '\0'
                    if (itemDiscount[countLineID][col] != null) {
                        return "A - " + (String) itemDiscount[countLineID][col] + " " + getItemDesignation((String) itemDiscount[countLineID][col]);
                    } else {
                        return null;
                    }

                case 1: // '\001'
                    if (itemDiscount[countLineID][col] != null) {
                        return ((BigDecimal) itemDiscount[countLineID][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 2: // '\002'
                    if (itemDiscount[countLineID][col] != null) {
                        return ((BigDecimal) itemDiscount[countLineID][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 3: // '\003'
                    if (itemDiscount[countLineID][col] != null) {
                        return ((BigDecimal) itemDiscount[countLineID][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 4: // '\004'
                    if (itemDiscount[countLineID][col] != null) {
                        return ((BigDecimal) itemDiscount[countLineID][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 6: // '\006'
                    countLineID++;
                    if (itemDiscount[countLineID - 1][col] != null) {
                        return ((BigDecimal) itemDiscount[countLineID - 1][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 5: // '\005'
                    if (itemDiscount[countLineID][col] != null) {
                        return ((BigDecimal) itemDiscount[countLineID][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 7: // '\007'
                    if (itemDiscount[countLineID][col] != null) {
                        return ((BigDecimal) itemDiscount[countLineID][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }
            }
            return null;
        }
        if (countLineFD < familyDiscount.length) {
            switch (col) {
                case 0: // '\0'
                    if (familyDiscount[countLineFD][col] != null) {
                        return "F - " + (String) familyDiscount[countLineFD][col];
                    } else {
                        return null;
                    }

                case 1: // '\001'
                    if (familyDiscount[countLineFD][col] != null) {
                        return ((BigDecimal) familyDiscount[countLineFD][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 2: // '\002'
                    if (familyDiscount[countLineFD][col] != null) {
                        return ((BigDecimal) familyDiscount[countLineFD][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 3: // '\003'
                    if (familyDiscount[countLineFD][col] != null) {
                        return ((BigDecimal) familyDiscount[countLineFD][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 4: // '\004'
                    if (familyDiscount[countLineFD][col] != null) {
                        return ((BigDecimal) familyDiscount[countLineFD][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 6: // '\006'
                    countLineFD++;
                    if (familyDiscount[countLineFD - 1][col] != null) {
                        return ((BigDecimal) familyDiscount[countLineFD - 1][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 5: // '\005'
                    if (familyDiscount[countLineFD][col] != null) {
                        return ((BigDecimal) familyDiscount[countLineFD][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 7: // '\007'
                    if (familyDiscount[countLineFD][col] != null) {
                        return ((BigDecimal) familyDiscount[countLineFD][col]).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }
            }
            return null;
        } else {
            return null;
        }
    }

    private String getItemDesignation(String itemId) {
        Item item = (Item) crud.read(itemId, 11);
        if (item != null) {
            return item.getDesignation();
        } else {
            return "";
        }
    }
    private Customer customer;
    private Object familyDiscount[][];
    private Object itemDiscount[][];
    private Parameter param;
    private int count;
    private int countLineFD;
    private int countLineID;
    private ObjectCRUD crud;
}
